package com.hydra.baseui.recyclerlist;

import android.view.View;
import android.view.ViewGroup;

public abstract class DefaultListAdapter<T> extends BaseListAdapter<T, BaseItemHolder> {

    protected ItemHolderBuilder mItemHolderBuilder;

    public DefaultListAdapter(ItemHolderBuilder itemBuilder) {
        setItemBuilder(itemBuilder);
    }

    public DefaultListAdapter(int... resources) {
        this(new ResourceItemHolderBuilder(resources));
    }

    /**
     * 这里clzs的顺序和itemtype要一一对应
     */
    public DefaultListAdapter(Class<? extends View>... clzs) {
        this(new ViewClassItemHolderBuilder(clzs));
    }

    public void setItemBuilder(ItemHolderBuilder builder) {
        if (null != mItemHolderBuilder) {
            mItemHolderBuilder.release();
        }
        mItemHolderBuilder = builder;
    }

    @Override
    public final BaseItemHolder buildViewHolder(ViewGroup parent, int viewType) {
        return mItemHolderBuilder.buildItem(parent, viewType);
    }

	@Override
    public void release() {
        super.release();

        if (null != mItemHolderBuilder) {
            mItemHolderBuilder.release();
        }
    }
}
